//
//  OrderVC.m
//  YaoHeAPP
//
//  Created by mac on 15/11/20.
//  Copyright (c) 2015年 mac. All rights reserved.
//

//*************************订单页面

#import "OrderVC.h"
#import "Controller.h"
#import "SVPullToRefresh.h"
#import "orderListData.h"
#import "ordermodel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "city.h"
#import "sheng.h"
#import "DatabaseTool.h"
#import "OrderNoImageTableViewCell.h"
#import "OrderwithImageTableViewCell.h"
#import "QCTableViewController.h"
#import "QuxiaoQCViewController.h"
#import "QClistViewController.h"
#import "OrderDetailesVC.h"
#import "OrderDetailesVC.h"
#import "ProvinceAndCity.h"
#import "CreateCompanyVC.h"
#import "Tool.h"
#import "labelview.h"
#import "FindCompanyViewController.h"
#import "CompanyInviteCodeViewController.h"
#import "YongHuQuanXuanModel.h"
#import "MJExtension.h"
#import "TongYong.h"

@interface OrderVC ()<UITableViewDataSource,UITableViewDelegate,labelviewdelegate>
{
    NSMutableArray *arrayList;//报价数组；

    orderListData *listdata;
    
    BOOL image11;

    NSString *shopNamelable;//cell的主要名称
    
    UILabel *caizhi;//材质
    
    NSInteger cellhight;//cell的高度
    
    NSInteger labhight;
    
    
    UIView *_bottmView;//底部试图
    UILabel *_zhijianxiang;//质检项

    labelview *la;//无数据显示的页面
 
    
}
@property(nonatomic,strong) UITableView *tableview;
@property (nonatomic, assign)NSInteger  page;//页数

@end

@implementation OrderVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"OrderVC"];
}


- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
//    [self.tableview triggerPullToRefresh];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"OrderVC"];
    
    //判断是否有创建公司的权限
    [self panduanCreate];
    
}
//判断是否创建公司
-(void)panduanCreate
{
    [Controller requestWithURL:POSTCHECK jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            YongHuQuanXuanModel *yongHuQuanXuanModel=[YongHuQuanXuanModel mj_objectWithKeyValues:data.data];
            NSString *IDnumString=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.COMPANY_ID];
            if ([IDnumString isEqualToString:@"0"]){
                NSString *statusSt=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.STATUS];
                if ([TongYong isBlankObjectAndStr:yongHuQuanXuanModel.STATUS]||[statusSt isEqualToString:@"-1"])  {
                    //没有值便是 申请
                    //没有绑定公司 （提示是否加入公司  或者  创建公司  或者 输入验证码 加入公司）
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:@"您还没有“创建/加入”一家公司，不能进行该操作"
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:@"创建公司",@"查找公司",@"输入邀请码",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 0;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                }else{
                    //0申请中  -1被拒绝
                    
                    NSString *messageSt=[NSString stringWithFormat:@"您于%@申请加入“%@”，公司主管理员尚未同意您加入公司。",yongHuQuanXuanModel.APPLY_TIME,yongHuQuanXuanModel.APPLY_COMPANY_NAME];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:messageSt
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消申请"
                                                          otherButtonTitles:@"关闭",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 100;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                    
                }
            
        }else{
            
        }
    }
    }];
    
    
}

//根据被点击按钮的索引处理点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==0) {
        if(buttonIndex==0){
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
            
        }else if (buttonIndex==1) {
            //创建公司
            CreateCompanyVC*creatVC=[CreateCompanyVC new];
            [self.navigationController pushViewController:creatVC animated:YES];
            
        }else if(buttonIndex==2){
            //查找公司
            FindCompanyViewController*findCompanyViewController=[FindCompanyViewController new];
            [self.navigationController pushViewController:findCompanyViewController animated:NO];
            
        }else if (buttonIndex==3){
            //输入邀请码
            CompanyInviteCodeViewController *companyInviteCodeViewController=[CompanyInviteCodeViewController new];
            [self.navigationController pushViewController:companyInviteCodeViewController animated:YES];
        }
        
    }else if(alertView.tag==100){
        if (buttonIndex==0) {
            //取消申请
            [self quxiaoshenqingBut];
        }else{
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
        }
        
    }
}
//取消加入公司申请
-(void)quxiaoshenqingBut{
    
    [Controller requestWithURL:PostCancelApply jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            ALERTSHOW(@"你已成功取消加入该公司的申请")
        }else{
            
            ALERTSHOW(data.message);
        }
    }];
    
}

- (void)willPresentAlertView:(UIAlertView *)alertView{
    
    for( UIView * view in alertView.subviews ){
        
        if( [view isKindOfClass:[UILabel class]] )
        {
            UILabel* label = (UILabel*) view;
            
            label.textAlignment = NSTextAlignmentLeft;
        }
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
   
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
//
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"订单列表";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    

    
    //定义数组
    arrayList=[[NSMutableArray alloc]init];
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,kNavigationBarHeight, self.view.frame.size.width, self.view.frame.size.height-kNavigationBarHeight)];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];

    
    //放置下拉上拉刷新控件
    __weak OrderVC *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
    // setup infinite scrolling
    [self.tableview addInfiniteScrollingWithActionHandler:^{
        [weakSelf insertRowAtBottom];
    }];
    
    [self insertRowAtTop];
    
}
-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)didClickButtonInBottomView
{
    RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
    [rootTabBarViewController changeRootViewControllerTo:2];

    
}

- (void)insertRowAtTop {
    __weak OrderVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    _page=1;
    NSNumber*number1=[NSNumber numberWithInteger:1];
    NSNumber*number2=[NSNumber numberWithInteger:10];
    NSDictionary*di=@{@"page_num":number1,@"page_step":number2};
    
    [Controller requestWithURL:ORDERLIST jsondic:di finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            ordermodel *orderMd=[[ordermodel alloc]initWith:data.data];
            
            [arrayList removeAllObjects];
            
            [arrayList addObjectsFromArray:orderMd.sale_infos];
            
            
            //判断 是否  有 无数据
            if (arrayList.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }

                la=[[labelview alloc]init];
                [la setimage:@"ic_no_data" tit:@"您最近三个月没有下单" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.delegate=self;
                [_tableview addSubview:la];
                
            }else{
                
                if (la) {
                    [la removeFromSuperview];
                }
                [self.tableview reloadData];
                
            }

        }
        else
        {
            
        }
    }];
    
    
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    
    
    [weakSelf.tableview.pullToRefreshView stopAnimating];
    
    
    
    
}


- (void)insertRowAtBottom {
    
    __weak OrderVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    
        
        _page=_page+1;
        NSNumber*number1=[NSNumber numberWithInteger:_page];
        NSNumber*number2=[NSNumber numberWithInteger:10];
        NSDictionary*di=@{@"page_num":number1,@"page_step":number2};
        
        [Controller requestWithURL:ORDERLIST jsondic:di finishBlock:^(ClassData* data) {
            if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
            {
                ordermodel *orderMd=[[ordermodel alloc]initWith:data.data];

                [arrayList addObjectsFromArray:orderMd.sale_infos];
                
                [self.tableview reloadData];
                
            }
            else
            {
                
            }
        }];

    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.infiniteScrollingView stopAnimating];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
   
    return arrayList.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    orderListData *listdata1=[[orderListData alloc ]initWith:arrayList[indexPath.row]];
    
 if(listdata1.item_infos.count!=0)
 {
 
     NSMutableArray *itemName1=[NSMutableArray array];
     for (NSDictionary *name in listdata1.item_infos) {
         
         [itemName1 addObject:name[@"ITEM_NAME"]];
         
     }
     
//     return labhight;
     return 5+(5+15)*(itemName1.count/4)+89+20;
 }
    
    
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"OrderListCell";
    
   listdata=[[orderListData alloc ]initWith:arrayList[indexPath.row]];
    

    //拼接大名称的字段
    
    shopNamelable=[NSString stringWithFormat:@"%@ %@/%@ %@",listdata.variety_name,listdata.material,listdata.surface_name,listdata.spec_code];
        
    
    
//质检数组需要下部view
        if(listdata.item_infos.count!=0)
        {
    
            //判断是否有图片
            //判断数据中是否有图片数据
            if (listdata.imageurl||listdata.imageurl.length!=0) {
                image11=YES;
            }else{
                image11=NO;
            }
            
            
            if (image11) {
                
                //自定义cell类有图片
                OrderwithImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                
                if (cell == nil) {
                    //通过xib的名称加载自定义的cell
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderwithImageTableViewCell" owner:self options:nil] lastObject];
                    
                }
                
                //定义订单号
                cell.orderid.text=[NSString stringWithFormat:@"订单号：%@",listdata.order_code];
                
                //表格名称
                cell.ordlistname.text=shopNamelable;
               
                
                //质检项
                NSInteger reportStatus;
                
                NSInteger isneedtest=[[NSString stringWithFormat:@"%@",listdata.isneedtesting] integerValue];
                
                if (isneedtest==0) {
                    cell.zhijianzhuangtai.hidden=YES;
                    
                }else{
                //判断数据质检状态
                if (listdata.report_status||listdata.report_status.length!=0) {
                    reportStatus=[listdata.report_status  integerValue];
                }else{
                    reportStatus=5;
                }
                
                //判断状态进行状态兑换
                if (reportStatus==-1) {
                    //质检取消
                    cell.zhijianzhuangtai.text=@"质检取消";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0  alpha:1];
                }else if(reportStatus==0)
                {   //质检中
                    cell.zhijianzhuangtai.text=@"质检中";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:81/255.0 green:194/255.0 blue:220/255.0  alpha:1];
                    
                }else if(reportStatus==10)
                {
                    //质检完成
                    cell.zhijianzhuangtai.text=@"质检完成";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:255/255.0 green:116/255.0 blue:82/255.0  alpha:1];
                    
                }else if(reportStatus==5)
                {
                    //质检申请中
                    cell.zhijianzhuangtai.text=@"质检申请中";
                    cell.zhijianzhuangtai.textColor=[UIColor greenColor];
                    
                }
            }
                NSString *weight=[NSString stringWithFormat:@"%@",listdata.weight];
                if (weight.length==0) {
                    //地址
                    NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
                    NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
                    cell.addare.text=[NSString stringWithFormat:@"%@%@",shengadd,citadd];
                }else{
                    //地址
                    NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
                    NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
                    cell.addare.text=[NSString stringWithFormat:@"%@%@   %@",shengadd,citadd,weight];
                
                }
                
                
                
                
                //图片
                [cell.orderImage  sd_setImageWithURL:[NSURL URLWithString:listdata.imageurl] placeholderImage:[UIImage imageNamed:@"ic_qiugou_quotation"]];
                
                
                //建立最底部可变试图（通过判断是否显示）
                _bottmView=[[UIView alloc]init];
                //                    WithFrame:CGRectMake(0, 89, self.view.frame.size.width, 40)];
                _bottmView.backgroundColor=[UIColor whiteColor];
                
               
                _zhijianxiang=[[UILabel alloc]init];
                _zhijianxiang.frame=CGRectMake(15, 5, 50, 15);
                _zhijianxiang.textColor=[UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0  alpha:1];
                _zhijianxiang.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小
                _zhijianxiang.numberOfLines=0;
                _zhijianxiang.text=@"质检项：";
                [_bottmView addSubview:_zhijianxiang];
                [cell addSubview:_bottmView];
                
                
                
                //先用字典读出其中的名称
                
                NSMutableArray *itemName=[NSMutableArray array];
                for (NSDictionary *name in listdata.item_infos) {
                    
                    [itemName addObject:name[@"ITEM_NAME"]];
                    
                }
                
                
                NSInteger jianju=10;
                CGRect txtFrame;
                for (NSInteger i=0; i<itemName.count; i++) {
                    caizhi=[[UILabel alloc]init];
                    [_bottmView addSubview:caizhi];
                    caizhi.text=itemName[i];
                    caizhi.layer.borderWidth=0.5;
                    caizhi.layer.borderColor=[UIColor grayColor].CGColor;
                    caizhi.font=[UIFont systemFontOfSize:12];
                    caizhi.textAlignment=NSTextAlignmentCenter;
                    caizhi.layer.cornerRadius = 2;
                    caizhi.numberOfLines = 0;
                    //自适应高度
                    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],};
                    CGSize textSize = [itemName[i] boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
                    if(i==0){
                        caizhi.frame=CGRectMake(jianju+70, 5+(5+15)*(i/4), textSize.width+6, 15);
                    }else{
                        
                        if (i%4==0) {
                            
                            caizhi.frame=CGRectMake(jianju+70, 5+(5+15)*(i/4), textSize.width+6, 15);
                            
                        }else{
                            
                            CGSize textSize1 = [itemName[i-1] boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
                            caizhi.frame=CGRectMake(txtFrame.origin.x+jianju+textSize1.width, 5+(5+15)*(i/4), textSize.width+6, 15);
                            
                        }
                        
                    }
                    
                    txtFrame=caizhi.frame;
                    
                }
                
                _bottmView.frame=CGRectMake(0, 89, self.view.frame.size.width,5+(5+15)*(itemName.count/4)+20);
                
                labhight=5+(5+15)*(itemName.count/4)+89+20;
                
                return cell;
            }
            
            //自定义cell类无图片
            OrderNoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cell == nil) {
                //通过xib的名称加载自定义的cell
                cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderNoImageTableViewCell" owner:self options:nil] lastObject];
            }
            
            //定义订单号
            cell.orderid.text=[NSString stringWithFormat:@"订单号：%@",listdata.order_code];
            
            //表格名称
            cell.ordlistname.text=shopNamelable;
            
            //质检项
            
            NSInteger reportStatus;
            NSInteger isneedtest=[[NSString stringWithFormat:@"%@",listdata.isneedtesting] integerValue];
            
            if (isneedtest==0) {
                cell.zhijianzhuangtai.hidden=YES;
                
            }else{
                //判断数据质检状态
                if (listdata.report_status||listdata.report_status.length!=0) {
                    reportStatus=[listdata.report_status  integerValue];
                }else{
                    reportStatus=5;
                }
                
                //判断状态进行状态兑换
                if (reportStatus==-1) {
                    //质检取消
                    cell.zhijianzhuangtai.text=@"质检取消";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0  alpha:1];
                }else if(reportStatus==0)
                {   //质检中
                    cell.zhijianzhuangtai.text=@"质检中";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:81/255.0 green:194/255.0 blue:220/255.0  alpha:1];
                    
                }else if(reportStatus==10)
                {
                    //质检完成
                    cell.zhijianzhuangtai.text=@"质检完成";
                    cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:255/255.0 green:116/255.0 blue:82/255.0  alpha:1];
                    
                }else if(reportStatus==5)
                {
                    //质检申请中
                    cell.zhijianzhuangtai.text=@"质检申请中";
                    cell.zhijianzhuangtai.textColor=[UIColor greenColor];
                    
                }
            }
            
            
            NSString *weight=[NSString stringWithFormat:@"%@",listdata.weight];
            if (weight.length==0) {
                //地址
                NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
                NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
                cell.addare.text=[NSString stringWithFormat:@"%@%@",shengadd,citadd];
            }else{
                //地址
                NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
                NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
                cell.addare.text=[NSString stringWithFormat:@"%@%@   %@",shengadd,citadd,weight];
                
            }
            //建立最底部可变试图（通过判断是否显示）
            _bottmView=[[UIView alloc]init];
            //                    WithFrame:CGRectMake(0, 89, self.view.frame.size.width, 40)];
            _bottmView.backgroundColor=[UIColor whiteColor];
            
            _bottmView.frame=CGRectMake(0, 89, self.view.frame.size.width, 40);
            _zhijianxiang=[[UILabel alloc]init];
            _zhijianxiang.frame=CGRectMake(15, 5, 50, 15);
            _zhijianxiang.textColor=[UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0  alpha:1];
            _zhijianxiang.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小
            _zhijianxiang.numberOfLines=0;
            _zhijianxiang.text=@"质检项：";
            [_bottmView addSubview:_zhijianxiang];
            [cell addSubview:_bottmView];
            
            
            
            //先用字典读出其中的名称
            NSMutableArray *itemName=[NSMutableArray array];
            for (NSDictionary *name in listdata.item_infos) {
                
                [itemName addObject:name[@"ITEM_NAME"]];
                
            }
            
            
            NSInteger jianju=10;
            CGRect txtFrame;
            for (NSInteger i=0; i<itemName.count; i++) {
                caizhi=[[UILabel alloc]init];
                [_bottmView addSubview:caizhi];
                caizhi.text=itemName[i];
                caizhi.layer.borderWidth=0.5;
                caizhi.layer.borderColor=[UIColor grayColor].CGColor;
                caizhi.font=[UIFont systemFontOfSize:12];
                caizhi.textAlignment=NSTextAlignmentCenter;
                caizhi.layer.cornerRadius = 2;
                caizhi.numberOfLines = 0;
                //自适应高度
                NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],};
                CGSize textSize = [itemName[i] boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
                if(i==0){
                    caizhi.frame=CGRectMake(jianju+70, 5+(5+15)*(i/4), textSize.width+6, 15);
                }else{
                    
                    if (i%4==0) {
                        
                         caizhi.frame=CGRectMake(jianju+70, 5+(5+15)*(i/4), textSize.width+6, 15);
                        
                    }else{
                    
                    CGSize textSize1 = [itemName[i-1] boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
                    caizhi.frame=CGRectMake(txtFrame.origin.x+jianju+textSize1.width, 5+(5+15)*(i/4), textSize.width+6, 15);
                    
                    }

                }
                
                txtFrame=caizhi.frame;
                
            }
            
             _bottmView.frame=CGRectMake(0, 89, self.view.frame.size.width,5+(5+15)*(itemName.count/4)+20);
            
             labhight=5+(5+15)*(itemName.count/4)+89+20;
            return cell;
        }

            
    //判断是否有图片
    //判断数据中是否有图片数据
    if (listdata.imageurl||listdata.imageurl.length!=0) {
        image11=YES;
    }else{
        image11=NO;
    }
    
    if (image11) {
        
        //自定义cell类有图片
        OrderwithImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            //通过xib的名称加载自定义的cell
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderwithImageTableViewCell" owner:self options:nil] lastObject];
            
        }
        
        //定义订单号
        cell.orderid.text=[NSString stringWithFormat:@"订单号：%@",listdata.order_code];
        
        //表格名称
        cell.ordlistname.text=shopNamelable;
        //质检项
        
        NSInteger reportStatus;
        NSInteger isneedtest=[[NSString stringWithFormat:@"%@",listdata.isneedtesting] integerValue];
        
        if (isneedtest==0) {
            cell.zhijianzhuangtai.hidden=YES;
            
        }else{
            //判断数据质检状态
            if (listdata.report_status||listdata.report_status.length!=0) {
                reportStatus=[listdata.report_status  integerValue];
            }else{
                reportStatus=5;
            }
            
            //判断状态进行状态兑换
            if (reportStatus==-1) {
                //质检取消
                cell.zhijianzhuangtai.text=@"质检取消";
                cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0  alpha:1];
            }else if(reportStatus==0)
            {   //质检中
                cell.zhijianzhuangtai.text=@"质检中";
                cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:81/255.0 green:194/255.0 blue:220/255.0  alpha:1];
                
            }else if(reportStatus==10)
            {
                //质检完成
                cell.zhijianzhuangtai.text=@"质检完成";
                cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:255/255.0 green:116/255.0 blue:82/255.0  alpha:1];
                
            }else if(reportStatus==5)
            {
                //质检申请中
                cell.zhijianzhuangtai.text=@"质检申请中";
                cell.zhijianzhuangtai.textColor=[UIColor greenColor];
                
            }
        }
        
        
        NSString *weight=[NSString stringWithFormat:@"%@",listdata.weight];
        if (weight.length==0) {
            //地址
            NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
            NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
            cell.addare.text=[NSString stringWithFormat:@"%@%@",shengadd,citadd];
        }else{
            //地址
            NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
            NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
            cell.addare.text=[NSString stringWithFormat:@"%@%@   %@",shengadd,citadd,weight];
            
        }


        
        //图片
        [cell.orderImage  sd_setImageWithURL:[NSURL URLWithString:listdata.imageurl] placeholderImage:[UIImage imageNamed:@"ic_qiugou_quotation"]];
        
        return cell;
    }
    
    //自定义cell类有图片
    OrderNoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        //通过xib的名称加载自定义的cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderNoImageTableViewCell" owner:self options:nil] lastObject];
    }
    
    //定义订单号
    cell.orderid.text=[NSString stringWithFormat:@"订单号：%@",listdata.order_code];
    
    //表格名称
    cell.ordlistname.text=shopNamelable;
    //质检项
    
    NSInteger reportStatus;
    NSInteger isneedtest=[[NSString stringWithFormat:@"%@",listdata.isneedtesting] integerValue];
    
    if (isneedtest==0) {
        cell.zhijianzhuangtai.hidden=YES;
        
    }else{
        //判断数据质检状态
        if (listdata.report_status||listdata.report_status.length!=0) {
            reportStatus=[listdata.report_status  integerValue];
        }else{
            reportStatus=5;
        }
        
        //判断状态进行状态兑换
        if (reportStatus==-1) {
            //质检取消
            cell.zhijianzhuangtai.text=@"质检取消";
            cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0  alpha:1];
        }else if(reportStatus==0)
        {   //质检中
            cell.zhijianzhuangtai.text=@"质检中";
            cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:81/255.0 green:194/255.0 blue:220/255.0  alpha:1];
            
        }else if(reportStatus==10)
        {
            //质检完成
            cell.zhijianzhuangtai.text=@"质检完成";
            cell.zhijianzhuangtai.textColor=[UIColor colorWithRed:255/255.0 green:116/255.0 blue:82/255.0  alpha:1];
            
        }else if(reportStatus==5)
        {
            //质检申请中
            cell.zhijianzhuangtai.text=@"质检申请中";
            cell.zhijianzhuangtai.textColor=[UIColor greenColor];
            
        }
    }
    
    
    NSString *weight=[NSString stringWithFormat:@"%@",listdata.weight];
    if (weight.length==0) {
        //地址
        NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
        NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
        cell.addare.text=[NSString stringWithFormat:@"%@%@",shengadd,citadd];
    }else{
        //地址
        NSString *shengadd=[ProvinceAndCity inputShengID:listdata.supply_province_id];
        NSString *citadd=[ProvinceAndCity inputCityID:listdata.supply_city_id];
        cell.addare.text=[NSString stringWithFormat:@"%@%@   %@",shengadd,citadd,weight];
        
    }

    
    
    return cell;
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

//    orderListData *listdata1=[[orderListData alloc ]initWith:arrayList[indexPath.row]];
//    
//    NSString *orderid=[NSString stringWithFormat:@"%@",listdata1.order_id];
//    
//    QClistViewController *qcview=[[QClistViewController alloc]init];
//    qcview.hidesBottomBarWhenPushed=YES;
//    qcview.orderid=orderid;
//    [self.navigationController pushViewController:qcview animated:YES];
    


    //进入详情页面

    OrderDetailesVC*detailsVC=[OrderDetailesVC new];

    orderListData *listdata1=[[orderListData alloc ]initWith:arrayList[indexPath.row]];
    NSString *orderid=listdata1.order_id;
    detailsVC.orderID=orderid;
    [self.navigationController pushViewController:detailsVC animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
